﻿/************************************************************************     
 * 类名  :  Widget
 * 描述  :  Widget组件的基类
 * 版本  :  v1.0         
 * 用户  :  wangxinlu  
 * 时间  :  2/22/2016 5:51:49 AM    
************************************************************************  
 * Copyright @ wh 2016 . All rights reserved.  
************************************************************************/ 

///<reference path="TimeStampCreator"/>   

namespace wh.ui {

    export abstract class Widget {

        protected id: string;

        public nodeTypeMap: Map<string, HTMLElement> = new Map<string, HTMLElement>();

        public domNode: HTMLElement;

        public attachElement: HTMLElement;

        public uiData: {} = null;

        constructor(attchElement: HTMLElement, template: string, uiData?: {}) {
            this.init(attchElement, template, uiData);
            WidgetManager.register(this);
        }

        protected init(attachElement: HTMLElement, template: string, uiData?: {}):void {
            if (attachElement.id != null || attachElement.id != undefined || attachElement.id != "")
                this.id = attachElement.id;
            else
                this.id = attachElement.tagName + TimeStampCreator.createStamp();

            this.attachElement = attachElement;

            if (template != null) {
                this.domNode = $(template)[0];
                this.nodeTypeMap = this.parseWidgetNodeType(this.domNode);
                $(this.domNode).appendTo($(this.attachElement));
            } else {
                this.domNode = this.attachElement;
            }

            if (uiData)
                this.uiData = uiData;
            this.initView();
        }

        protected abstract initView(): void;

        public setVisible(visible: boolean) {
            $(this.domNode).css("display", visible ? "block" : "none");
        }

        protected parseWidgetNodeType(root: HTMLElement): Map<string,HTMLElement> {
            var resultMap: Map<string, HTMLElement> = new Map<string, HTMLElement>();

            $(root).find('div').each(function () {
                var nodeType = $(this).attr("ui-widget-NodeType");
                if (nodeType != null || nodeType != undefined)
                    resultMap.set(nodeType, $(this)[0]);
            });

            $(root).find('button').each(function () {
                var nodeType = $(this).attr("ui-widget-NodeType");
                if (nodeType != null || nodeType != undefined)
                    resultMap.set(nodeType, $(this)[0]);
            });

            $(root).find('input').each(function () {
                var nodeType = $(this).attr("ui-widget-NodeType");
                if (nodeType != null || nodeType != undefined)
                    resultMap.set(nodeType, $(this)[0]);
            });

            $(root).find('textArea').each(function () {
                var nodeType = $(this).attr("ui-widget-NodeType");
                if (nodeType != null || nodeType != undefined)
                    resultMap.set(nodeType, $(this)[0]);
            });

            return resultMap;
        }

        public getId(): string {
            return this.id;
        }

        public destroy(): void {
            $(this.domNode).remove();
            $(this.domNode).empty();
            $(this.domNode).off();

            WidgetManager.unRegister(this.id);
        }
    }

    
}